var validateRegExp = {
	decmal:"^([+-]?)\\d*\\.\\d+$", //浮点数
	decmal1: "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$", //正浮点数
	decmal2: "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$", //负浮点数
	decmal3: "^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$", //浮点数
	decmal4: "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$", //非负浮点数（正浮点数 + 0）
	decmal5: "^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$", //非正浮点数（负浮点数 + 0）
	intege: "^-?[1-9]\\d*$", //整数
	intege1: "^[1-9]\\d*$", //正整数
	intege2: "^-[1-9]\\d*$", //负整数
	num: "^([+-]?)\\d*\\.?\\d+$", //数字
	num1: "^[1-9]\\d*|0$", //正数（正整数 + 0）
	num2: "^-[1-9]\\d*|0$", //负数（负整数 + 0）
	ascii: "^[\\x00-\\xFF]+$", //仅ACSII字符
	chinese: "^[\\u4e00-\\u9fa5]+$", //仅中文
	color: "^[a-fA-F0-9]{6}$", //颜色
	date: "^\\d{4}(\\-|\\/|\.)\\d{1,2}\\1\\d{1,2}$", //日期
	email: "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", //邮件
	idcard: "^[1-9]([0-9]{14}|[0-9]{17})$", //身份证
	ip4: "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$", //ip地址
	letter: "^[A-Za-z]+$", //字母
	letter_l: "^[a-z]+$", //小写字母
	letter_u: "^[A-Z]+$", //大写字母
	mobile: "0?(13|15)[0-9]{9}$", //手机
	notempty: "^\\S+$", //非空
	password: "^[A-Za-z0-9_-]+$", //密码
	picture: "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$", //图片
	qq: "^[1-9]*[1-9][0-9]*$", //QQ号码
	rar: "(.*)\\.(rar|zip|7zip|tgz)$", //压缩文件
	tel: "^[0-9\-()（）]{7,18}$", //电话号码的函数(包括验证国内区号,国际区号,分机号)
	url: "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$", //url
	username: "^[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+$", //用户名
	deptname: "^[A-Za-z0-9_()（）\\-\\u4e00-\\u9fa5]+$", //单位名
	zipcode: "^\\d{6}$", //邮编
	realname:"^[A-Za-z0-9\\u4e00-\\u9fa5]+$", // 真实姓名
	companyname:"^[A-Za-z0-9_()（）\\-\\u4e00-\\u9fa5]+$",
	companyaddr:"^[A-Za-z0-9_()（）\\#\\-\\u4e00-\\u9fa5]+$",
	companysite:"^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&#=]*)?$"
};
//主函数
(function ($) {
	$.fn.jdValidate = function (option, callback, def) {
		var ele = this;
		var id = ele.attr("id");
		var type = ele.attr("type");
		var rel = ele.attr("rel");
		var _onFocus = $("#" + id + validateSettings.onFocus.container);
		var _succeed = $("#" + id + validateSettings.succeed.container);
		var _isNull = $("#" + id + validateSettings.isNull.container);
		var _error = $("#" + id + validateSettings.error.container);
		if (def == true) {
			var str = ele.val();
			var tag = ele.attr("sta");
			if (str == "" || str == "-1") {
				validateSettings.isNull.run({prompts:option, element:ele, isNullEle:_isNull, succeedEle:_succeed}, option.isNull);
			} else {
				if (tag == 1 || tag == 2) {
					return;
				} else {
					callback({prompts:option, element:ele, value:str, errorEle:_error, succeedEle:_succeed});
				}
			}
		} else {
			if (typeof def == "string") {
				ele.val(def);
			}
			if (type == "checkbox" || type == "radio") {
				if (ele.attr("checked") == true) {
					ele.attr("sta", validateSettings.succeed.state);
				}
			}
			switch (type) {
			  case "text":
			  case "password":
				ele.bind("focus", function () {
					var str = ele.val();
					if (str == def) {
						ele.val("");
					}
					validateSettings.onFocus.run({prompts:option, element:ele, value:str, onFocusEle:_onFocus, succeedEle:_succeed}, option.onFocus);
				}).bind("blur", function () {
					var str = ele.val();
					if (str == "") {
						ele.val(def);
					}
					if (validateRules.isNull(str)) {
						validateSettings.isNull.run({prompts:option, element:ele, value:str, isNullEle:_isNull, succeedEle:_succeed}, "");
					} else {
						callback({prompts:option, element:ele, value:str, errorEle:_error, isNullEle:_isNull, succeedEle:_succeed});
					}
				});
				break;
			  default:
				if (rel && rel == "select") {
					ele.bind("change", function () {
						var str = ele.val();
						callback({prompts:option, element:ele, value:str, errorEle:_error, isNullEle:_isNull, succeedEle:_succeed});
					});
				} else {
					ele.bind("click", function () {
						callback({prompts:option, element:ele, errorEle:_error, isNullEle:_isNull, succeedEle:_succeed});
					});
				}
				break;
			}
		}
	};
})(jQuery);
//配置
var validateSettings = {onFocus:{state:null, container:"_error", style:"focus", run:function (option, str) {
	if (!validateRules.checkType(option.element)) {
		option.element.removeClass(validateSettings.INPUT_style2).addClass(validateSettings.INPUT_style1);
	}
	option.onFocusEle.removeClass().addClass(validateSettings.onFocus.style).html(str);
}}, isNull:{state:0, container:"_error", style:"null", run:function (option, str) {
	option.element.attr("sta", 0);
	if (!validateRules.checkType(option.element)) {
		if (str != "") {
			option.element.removeClass(validateSettings.INPUT_style1).addClass(validateSettings.INPUT_style2);
		} else {
			option.element.removeClass(validateSettings.INPUT_style2).removeClass(validateSettings.INPUT_style1);
		}
	}
	option.succeedEle.removeClass(validateSettings.succeed.style);
	option.isNullEle.removeClass().addClass(validateSettings.isNull.style).html(str);
}}, error:{state:1, container:"_error", style:"error", run:function (option, str) {
	option.element.attr("sta", 1);
	if (!validateRules.checkType(option.element)) {
		option.element.removeClass(validateSettings.INPUT_style1).addClass(validateSettings.INPUT_style2);
	}
	option.succeedEle.removeClass(validateSettings.succeed.style);
	option.errorEle.removeClass().addClass(validateSettings.error.style).html(str);
}}, succeed:{state:2, container:"_succeed", style:"succeed", run:function (option) {
	option.element.attr("sta", 2);
	option.errorEle.empty();
	if (!validateRules.checkType(option.element)) {
		option.element.removeClass(validateSettings.INPUT_style1).removeClass(validateSettings.INPUT_style2);
	}
	if (option.element.attr("id") == "schoolinput" && $("#schoolid").val() == "") {
		return;
	}
	option.succeedEle.addClass(validateSettings.succeed.style);
}}, INPUT_style1:"highlight1", INPUT_style2:"highlight2"};
//验证规则
var validateRules = {isNull:function (str) {
	return (str == "" || typeof str != "string");
}, betweenLength:function (str, _min, _max) {
	return (str.length >= _min && str.length <= _max);
}, isUid:function (str) {
	return new RegExp(validateRegExp.username).test(str);
}, isPwd:function (str) {
	return new RegExp(validateRegExp.password).test(str);
}, isPwd2:function (str1, str2) {
	return (str1 == str2);
}, isEmail:function (str) {
	return new RegExp(validateRegExp.email).test(str);
}, isTel:function (str) {
	return new RegExp(validateRegExp.tel).test(str);
}, pwd2:{onFocus:"\u8bf7\u518d\u6b21\u8f93\u5165\u5bc6\u7801", succeed:"", isNull:"\u8bf7\u8f93\u5165\u5bc6\u7801", error:{badLength:"\u5bc6\u7801\u957f\u5ea6\u53ea\u80fd\u57286-16\u4f4d\u5b57\u7b26\u4e4b\u95f4", badFormat2:"\u4e24\u6b21\u8f93\u5165\u5bc6\u7801\u4e0d\u4e00\u81f4", badFormat1:"\u5bc6\u7801\u53ea\u80fd\u7531\u82f1\u6587\u3001\u6570\u5b57\u53ca\"_\"\u3001\"-\"\u7ec4\u6210"}}, mail:{onFocus:"\u8bf7\u8f93\u5165\u5e38\u7528\u7684\u90ae\u7bb1\uff0c\u5c06\u7528\u6765\u627e\u56de\u5bc6\u7801\u3001\u63a5\u6536\u8ba2\u5355\u901a\u77e5\u7b49", succeed:"", isNull:"\u8bf7\u8f93\u5165\u90ae\u7bb1", error:{beUsed:"\u8be5\u90ae\u7bb1\u5df2\u88ab\u4f7f\u7528\uff0c\u8bf7\u66f4\u6362\u5176\u5b83\u90ae\u7bb1\uff0c\u6216\u4f7f\u7528\u8be5\u90ae\u7bb1<a href='http://passport.360buy.com/retrievepassword.aspx' class='flk13'>\u627e\u56de\u5bc6\u7801</a>", badFormat:"\u90ae\u7bb1\u683c\u5f0f\u4e0d\u6b63\u786e", badLength:"\u60a8\u586b\u5199\u7684\u90ae\u7bb1\u8fc7\u957f\uff0c\u90ae\u4ef6\u5730\u5740\u53ea\u80fd\u572850\u4e2a\u5b57\u7b26\u4ee5\u5185"}}, authcode:{onFocus:"\u8bf7\u8f93\u5165\u56fe\u7247\u4e2d\u7684\u5b57\u7b26\uff0c\u4e0d\u533a\u5206\u5927\u5c0f\u5199", succeed:"", isNull:"\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801", error:"\u9a8c\u8bc1\u7801\u9519\u8bef"}, protocol:{onFocus:"", succeed:"", isNull:"\u8bf7\u5148\u9605\u8bfb\u5e76\u540c\u610f\u300a\u4eac\u4e1c\u5546\u57ce\u7528\u6237\u534f\u8bae\u300b", error:""}, referrer:{onFocus:"\u5982\u60a8\u6ce8\u518c\u5e76\u5b8c\u6210\u8ba2\u5355\uff0c\u63a8\u8350\u4eba\u6709\u673a\u4f1a\u83b7\u5f97\u79ef\u5206", succeed:"", isNull:"", error:""}, schoolinput:{onFocus:"\u60a8\u53ef\u4ee5\u7528\u7b80\u62fc\u3001\u5168\u62fc\u3001\u4e2d\u6587\u8fdb\u884c\u6821\u540d\u6a21\u7cca\u67e5\u627e", succeed:"", isNull:"\u8bf7\u586b\u9009\u5b66\u6821\u540d\u79f0", error:"\u8bf7\u586b\u9009\u5b66\u6821\u540d\u79f0"}, empty:{onFocus:"", succeed:"", isNull:"", error:""}};
var nameold, emailold, authcodeold;
var namestate = false, emailstate = false, authcodestate = false;
//回调函数
var validateFunction = {username:function (option) {
	var format = validateRules.isUid(option.value);
	var length = validateRules.betweenLength(option.value.replace(/[^\x00-\xff]/g, "**"), 4, 20);
	if (!length && format) {
		validateSettings.error.run(option, option.prompts.error.badLength);
	} else {
		if (!length && !format) {
			validateSettings.error.run(option, option.prompts.error.badFormat);
		} else {
			if (length && !format) {
				validateSettings.error.run(option, option.prompts.error.badFormat);
			} else {
				if (!namestate || nameold != option.value) {
					if (nameold != option.value) {
						nameold = option.value;
						option.errorEle.html("<span style='color:#999'>\u68c0\u9a8c\u4e2d\u2026\u2026</span>");
						$.getJSON("AjaxService.aspx?action=CheckUnicknme&uid=" + escape(option.value) + "&r=" + Math.random(), function (date) {
							if (date.success == 0) {
								validateSettings.succeed.run(option);
								namestate = true;
							} else {
								validateSettings.error.run(option, option.prompts.error.beUsed.replace("{1}", option.value));
								namestate = false;
							}
						});
					} else {
						validateSettings.error.run(option, option.prompts.error.beUsed.replace("{1}", option.value));
						namestate = false;
					}
				} else {
					validateSettings.succeed.run(option);
				}
			}
		}
	}
}, pwd:function (option) {
	var str1 = option.value;
	var str2 = $("#pwd2").val();
	var format = validateRules.isPwd(option.value);
	var length = validateRules.betweenLength(option.value, 6, 16);
	$("#pwdstrength").hide();
	if (!length && format) {
		validateSettings.error.run(option, option.prompts.error.badLength);
	} else {
		if (!length && !format) {
			validateSettings.error.run(option, option.prompts.error.badFormat);
		} else {
			if (length && !format) {
				validateSettings.error.run(option, option.prompts.error.badFormat);
			} else {
				validateSettings.succeed.run(option);
				validateFunction.pwdstrength();
			}
		}
	}
	if (str2 != "") {
		$("#pwd2").jdValidate(validatePrompt.pwd2, validateFunction.pwd2, true);
	}
}, pwd2:function (option) {
	var str1 = option.value;
	var str2 = $("#pwd").val();
	var length = validateRules.betweenLength(option.value, 6, 16);
	var format2 = validateRules.isPwd2(str1, str2);
	var format1 = validateRules.isPwd(str1);
	if (!length) {
		validateSettings.error.run(option, option.prompts.error.badLength);
	} else {
		if (!format1) {
			validateSettings.error.run(option, option.prompts.error.badFormat1);
		} else {
			if (!format2) {
				validateSettings.error.run(option, option.prompts.error.badFormat2);
			} else {
				validateSettings.succeed.run(option);
			}
		}
	}
}, mail:function (option) {
	var format = validateRules.isEmail(option.value);
	var format2 = validateRules.betweenLength(option.value, 0, 50);
	if (!format) {
		validateSettings.error.run(option, option.prompts.error.badFormat);
	} else {
		if (!format2) {
			validateSettings.error.run(option, option.prompts.error.badLength);
		} else {
			if (!emailstate || emailold != option.value) {
				if (emailold != option.value) {
					emailold = option.value;
					option.errorEle.html("<span style='color:#999'>\u68c0\u9a8c\u4e2d\u2026\u2026</span>");
					$.getJSON("AjaxService.aspx?action=CheckUemail&str=" + escape(option.value) + "&r=" + Math.random(), function (date) {
						if (date.success == 0) {
							validateSettings.succeed.run(option);
							emailstate = true;
						} else {
							validateSettings.error.run(option, option.prompts.error.beUsed);
							emailstate = false;
						}
					});
				} else {
					validateSettings.error.run(option, option.prompts.error.beUsed);
					emailstate = false;
				}
			} else {
				validateSettings.succeed.run(option);
			}
		}
	}
}, referrer:function (option) {
	var bool = validateRules.isNull(option.value);
	if (bool) {
		option.element.val("\u53ef\u60f6\ufffd");
		return;
	} else {
		validateSettings.succeed.run(option);
	}
}, schoolinput:function (option) {
	var bool = validateRules.isNull(option.value);
	if (bool) {
		validateSettings.error.run(option, option.prompts.error);
		return;
	} else {
		validateSettings.succeed.run(option);
	}
}, authcode:function (option) {
	if (!authcodestate || authcodeold != option.value) {
		if (authcodeold != option.value) {
			authcodeold = option.value;
			option.errorEle.html("<span style='color:#999'>\u68c0\u9a8c\u4e2d\u2026\u2026</span>");
			var uuid = $("#JD_Verification1").attr("src").split("&uid=")[1].split("&")[0];
			$.getJSON("AjaxService.aspx?action=CheckAuthcode&str=" + escape(option.value) + "&r=" + Math.random() + "&uuid=" + uuid, function (date) {
				if (date.success == 0) {
					validateSettings.succeed.run(option);
					authcodestate = true;
				} else {
					validateSettings.error.run(option, option.prompts.error);
					authcodestate = false;
				}
			});
		} else {
			validateSettings.error.run(option, option.prompts.error);
			authcodestate = false;
		}
	} else {
		validateSettings.succeed.run(option);
	}
}, protocol:function (option) {
	if (option.element.attr("checked") == true) {
		option.element.attr("sta", validateSettings.succeed.state);
		option.errorEle.html("");
	} else {
		option.element.attr("sta", validateSettings.isNull.state);
		option.succeedEle.removeClass(validateSettings.succeed.style);
	}
}, pwdstrength:function () {
	var element = $("#pwdstrength");
	var value = $("#pwd").val();
	var strength = 0;
	if (value.length >= 6 && validateRules.isPwd(value)) {
		$("#pwd_error").empty();
		element.show();
		if (/\d/i.test(value)) {
			strength += 1;
		}
		if (/[a-z]/i.test(value)) {
			strength += 1;
		}
		if (/[-_]/i.test(value)) {
			strength += 1;
		}
		switch (strength) {
		  case 1:
			element.removeClass().addClass("strengthA");
			break;
		  case 2:
			element.removeClass().addClass("strengthB");
			break;
		  case 3:
			element.removeClass().addClass("strengthC");
			break;
		  default:
			break;
		}
	} else {
		element.hide();
	}
}, checkGroup:function (elements) {
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].checked) {
			return true;
		}
	}
	return false;
}, checkSelectGroup:function (elements) {
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].value == -1) {
			return false;
		}
	}
	return true;
}, showPassword:function (type) {
	var v1 = $("#pwd").val(), s1 = $("#pwd").attr("sta"), c1 = document.getElementById("pwd").className, t1 = $("#pwd").attr("tabindex");
	var v2 = $("#pwd2").val(), s2 = $("#pwd2").attr("sta"), c2 = document.getElementById("pwd2").className, t2 = $("#pwd2").attr("tabindex");
	var P1 = $("<input type='" + type + "' value='" + v1 + "' sta='" + s1 + "' class='" + c1 + "' id='pwd' name='pwd' tabindex='" + t1 + "'/>");
	$("#pwd").after(P1).remove();
	$("#pwd").bind("keyup", function () {
		validateFunction.pwdstrength();
	}).jdValidate(validatePrompt.pwd, validateFunction.pwd);
	var P2 = $("<input type='" + type + "' value='" + v2 + "' sta='" + s2 + "' class='" + c2 + "' id='pwd2' name='pwd2' tabindex='" + t2 + "'/>");
	$("#pwd2").after(P2).remove();
	$("#pwd2").jdValidate(validatePrompt.pwd2, validateFunction.pwd2);
}, FORM_submit:function (elements) {
	var bool = true;
	for (var i = 0; i < elements.length; i++) {
		if ($(elements[i]).attr("sta") == 2) {
			bool = true;
		} else {
			bool = false;
			break;
		}
	}
	return bool;
}};

